package com.yanqu.road.server.manger.maidian.wingroom;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.wingroom.UserFurnitureSample;
import com.yanqu.road.entity.wingroom.UserFurnitureSuitData;
import com.yanqu.road.entity.wingroom.config.WingRoomFurnitureConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;
import com.yanqu.road.server.manger.wingroom.WingRoomConfigMgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WingRoomLog {

    private static String WING_ROOM = "wing_room";
    private static String WING_CONTENT = "wing_content";
    private static String WING_ADD = "wing_add";
    private static String WING_LEVEL = "wing_level";
    private static String WING_ID = "wing_id";
    private static String WING_LV = "wing_lv";
    private static String WING_QUALITY = "wing_quality";
    private static String WING_BELONG = "wing_belong";
    private static String VOCATION = "vocation";                     //职业
    private static String ADD_BASE = "add_base";                     //赚钱固定值
    private static String ADD_WING = "add_wing";                     //赚钱百分比
    private static String WING_INTELLIGENCE = "wing_intelligence";   //资质


    public static void uploadSnapshot(GamePlayer player) {

        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            DefaultEventTraceAction action = new DefaultEventTraceAction(WING_ROOM, player);

            List<Map<String, Object>> objectGroup = new ArrayList<>();
            WingRoomModule wingRoomModule = player.getModule(WingRoomModule.class);
            for (Map.Entry<Integer, UserFurnitureSample> sampleEntry : wingRoomModule.getFurnitureMap().entrySet()) {
                UserFurnitureSample sample = sampleEntry.getValue();
                Map<String, Object> map = new HashMap<>();
                map.put(WING_ID, sample.getFurnitureId());
                map.put(WING_LV, sample.getLv());
                WingRoomFurnitureConfig furnitureConfig = WingRoomConfigMgr.getFurnitureConfig(sample.getFurnitureId());
                if(furnitureConfig != null){
                    map.put(WING_QUALITY, furnitureConfig.getQuality());
                    map.put(WING_BELONG, furnitureConfig.getSuitType());
                }
                objectGroup.add(map);
            }
            action.build(WING_CONTENT, objectGroup);

            //职业，固定赚钱
            Map<Integer, Long> occupationEarnAdd = new HashMap<>();
            //职业，赚钱百分比加成
            Map<Integer, Long> occupationEarnAddition = new HashMap<>();
            //职业，资质加成
            Map<Integer, Long> occupationZiZhi = new HashMap<>();

            for (UserFurnitureSuitData suitData : wingRoomModule.getSuitDataMap().values()) {
                Integer skillId = ConfigMgr.getSuitSkillMap().get(suitData.getSuitType());
                if (skillId == null) {
                    continue;
                }
                SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
                if (skillInfo == null) {
                    continue;
                }
                //家具套装加门客赚钱千分比
                if (skillInfo.getType() == eSkillType.SpecialOccupationPatronsAbilityAddRatio.getValue()) {
                    int occupation = skillInfo.getParamList().get(2);
                    int addValue = SkillMgr.getSkillAddition(skillInfo, suitData.getSuitLv());
                    long old = occupationEarnAddition.getOrDefault(occupation, 0L);
                    occupationEarnAddition.put(occupation, old + addValue);
                }
                //资质
                if (skillInfo.getType() == eSkillType.SkinBeautySkillAddPatronsAbility.getValue()) {
                    int occupation = skillInfo.getParamList().get(2);
                    int addValue = SkillMgr.getSkillAddition(skillInfo, suitData.getSuitLv());
                    long old = occupationZiZhi.getOrDefault(occupation, 0L);
                    occupationZiZhi.put(occupation, old + addValue);
                }
            }

            for (UserFurnitureSample sample : wingRoomModule.getFurnitureMap().values()) {
                WingRoomFurnitureConfig furnitureConfig = WingRoomConfigMgr.getFurnitureConfig(sample.getFurnitureId());
                if (furnitureConfig == null) {
                    continue;
                }
                for (Integer skillId : furnitureConfig.getSkills()) {
                    SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
                    if (skillInfo == null) {
                        continue;
                    }
                    //家具赚钱加成固定值
                    if (skillInfo.getType() == eSkillType.PatronsCookSkill.getValue()) {
                        int occupation = skillInfo.getParamList().get(2);
                        int addValue = SkillMgr.getSkillAddition(skillInfo, sample.getLv());
                        long old = occupationEarnAdd.getOrDefault(occupation, 0L);
                        occupationEarnAdd.put(occupation, old + addValue);
                    }
                    //家具资质加成固定值
                    if (skillInfo.getType() == eSkillType.SkinBeautySkillAddPatronsAbility.getValue()) {
                        int occupation = skillInfo.getParamList().get(2);
                        int addValue = SkillMgr.getSkillAddition(skillInfo, sample.getLv());
                        long old = occupationZiZhi.getOrDefault(occupation, 0L);
                        occupationZiZhi.put(occupation, old + addValue);
                    }
                }
            }

            objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, Long> entry : occupationEarnAdd.entrySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put(VOCATION, entry.getKey());
                map.put(ADD_BASE, entry.getValue());
                objectGroup.add(map);
            }
            for (Map.Entry<Integer, Long> entry : occupationEarnAddition.entrySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put(VOCATION, entry.getKey());
                map.put(ADD_WING, entry.getValue());
                objectGroup.add(map);
            }
            for (Map.Entry<Integer, Long> entry : occupationZiZhi.entrySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put(VOCATION, entry.getKey());
                map.put(WING_INTELLIGENCE, entry.getValue());
                objectGroup.add(map);
            }

            action.build(WING_ADD, objectGroup);
            if(wingRoomModule.getWingRoomData() != null) {
                action.build(WING_LEVEL, wingRoomModule.getWingRoomData().getLv());
            }
            action.trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }
}
